﻿using System;
using System.Threading;
using System.Threading.Tasks;
using TiffLibrary;

namespace WholeSlideImageLibrary.Formats.GenericTiff
{
    internal class FileSourceReaderSimpleContentSource : TiffFileContentSource
    {
        private readonly ContentReader _contentReader;

        public FileSourceReaderSimpleContentSource(FileSourceReader reader)
        {
            _contentReader = new ContentReader(reader);
        }

        public override TiffFileContentReader OpenReader()
        {
            throw new NotSupportedException();
        }

        public override ValueTask<TiffFileContentReader> OpenReaderAsync(CancellationToken cancellationToken = default)
        {
            return new ValueTask<TiffFileContentReader>(_contentReader);
        }

        protected override void Dispose(bool disposing) { }

        internal sealed class ContentReader : TiffFileContentReader
        {
            private readonly FileSourceReader _reader;

            public ContentReader(FileSourceReader reader)
            {
                _reader = reader;
            }

            public override int Read(TiffStreamOffset offset, Memory<byte> buffer)
            {
                throw new NotSupportedException();
            }

            public override ValueTask<int> ReadAsync(TiffStreamOffset offset, ArraySegment<byte> buffer, CancellationToken cancellationToken = default)
            {
                return _reader.ReadAsync(offset, buffer, cancellationToken);
            }

            public override ValueTask<int> ReadAsync(TiffStreamOffset offset, Memory<byte> buffer, CancellationToken cancellationToken = default)
            {
                return _reader.ReadAsync(offset, buffer, cancellationToken);
            }

            protected override void Dispose(bool disposing) { }
        }
    }
}
